#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "textstream.h"

static const char* text = "The quick brown fox jumped over the slow lazy dog.";
static const F437Color colors[] = {
  F437_COLOR_BLUE,
  F437_COLOR_GREEN,
  F437_COLOR_CYAN,
  F437_COLOR_RED,
  F437_COLOR_MAGENTA,
  F437_COLOR_YELLOW,
  F437_COLOR_WHITE,
  F437_COLOR_BBLACK,
  F437_COLOR_BBLUE,
  F437_COLOR_BGREEN,
  F437_COLOR_BCYAN,
  F437_COLOR_BRED,
  F437_COLOR_BMAGENTA,
  F437_COLOR_BYELLOW,
  F437_COLOR_BWHITE,
  F437_COLOR_TRANSPARENT
};

int main(int argc, char *argv[]) {
  (void)argc;
  (void)argv;

  SDL_Init(SDL_INIT_VIDEO);
  const F437Font* font = f437_get_font(F437_FONT_8x14);
  SDL_Surface* screen =
    SDL_SetVideoMode(F437_DEFAULT_WIDTH * font->char_width,
                     F437_DEFAULT_HEIGHT * font->char_height,
                     32,
                     SDL_SWSURFACE | SDL_DOUBLEBUF);
  struct textstream* ts = textstream_new(screen, font, 1);
  for (gint i = 0; i < 5; i++) {
    const F437Color* color = colors;
    while (*color != F437_COLOR_TRANSPARENT) {
      textstream_fg(ts, *color);
      textstream_add(ts, text);
      color++;
    }
  }
  gboolean done = FALSE;
  gboolean cleared = FALSE;
  while (!done) {
    SDL_Event ev;
    while (SDL_PollEvent(&ev)) {
      if (ev.type == SDL_QUIT) {
        done = TRUE;
        break;
      }
    }

    if (textstream_update(ts)) {
      SDL_Flip(screen);
    } else {
      if (!cleared) {
        cleared = TRUE;
        textstream_clear(ts);
        textstream_add(ts, text);
      }
    }
  }
  textstream_free(ts);
  SDL_Quit();
  return 0;
}
